package com.example.taximeter.service.impl;

import com.example.taximeter.bean.*;
import com.example.taximeter.mapper.FactMapper;
import com.example.taximeter.mapper.ParamFileMapper;
import com.example.taximeter.mapper.TaximeterMapper;
import com.example.taximeter.mapper.TaximeterTypeMapper;
import com.example.taximeter.service.TaximeterTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 计价器型号
 */
@Service
public class TaximeterTypeServiceImpl implements TaximeterTypeService {

    private TaximeterTypeMapper taximeterTypeMapper;
    private TaximeterMapper taximeterMapper;
    private ParamFileMapper paramFileMapper;
    private FactMapper factMapper;

    @Autowired
    public TaximeterTypeServiceImpl(TaximeterTypeMapper taximeterTypeMapper,TaximeterMapper taximeterMapper,ParamFileMapper paramFileMapper,FactMapper factMapper){
        this.taximeterTypeMapper = taximeterTypeMapper;
        this.taximeterMapper = taximeterMapper;
        this.paramFileMapper = paramFileMapper;
        this.factMapper = factMapper;
    }

    @Override
    public List<TaximeterType> findTaximeterTypeList() {

        return taximeterTypeMapper.findTaximeterTypeList();
    }

    @Override
    public String findJjqtypeByJjqtypeid(Integer id) {
        return taximeterTypeMapper.findJjqtypeByJjqtypeid(id);
    }


    @Override
    public List<TaximeterType> findJjqtypeByFactname(String factname) {
        return taximeterTypeMapper.findJjqByFactname(factname);
    }

    @Override
    public List<TaximeterType> findJjqtypeByFactid(Integer factid) {
        return taximeterTypeMapper.findJjqByFactid(factid);
    }

    @Override
    public ReturnInfo findTaximeterTypeAll(Integer pageNum, Integer pageSize, TaximeterTypeView taximeterTypeView) {
        if(!pageNum.equals(0)||!pageSize.equals(0)){
            PageHelper.startPage(pageNum, pageSize);
        }
        Integer pagetotal = 0;
        Long total = 0L;
        List<TaximeterTypeView> taximeterTypeViews = taximeterTypeMapper.findTaximeterTypeAll(taximeterTypeView);
        PageInfo<TaximeterTypeView> pageInfo = new PageInfo<>(taximeterTypeViews);
        if(taximeterTypeViews.size()!=0){
            pagetotal = pageInfo.getPages();
            total = pageInfo.getTotal();
        }
        return ReturnInfo.info5(pagetotal,total,pageInfo.getList());
    }

    @Override
    public String addTaximeterType(TaximeterType taximeterType) {
        if(taximeterTypeMapper.findTaximeterTypeByTaximeterType(taximeterType.getJjqtype()).size()!=0){
            return "计价器型号重复";
        }else if(taximeterTypeMapper.addTaximeterType(taximeterType)){
            return "成功";
        }else {
            return "失败";
        }

    }

    @Override
    public String updateTaximeterType(TaximeterTypeView taximeterTypeView) {
        List<TaximeterType> taximeterTypes = taximeterTypeMapper.findTaximeterTypeByTaximeterType(taximeterTypeView.getJjqtype());
        if(taximeterTypes.size()==1){
            if(taximeterTypes.get(0).getId()!=taximeterTypeView.getId()){
                return "计价器型号重复";
            }else {
                //因为是由名称查到id，这里是如果名称不唯一也可以找到一个id
                if(factMapper.findFactByFactname(taximeterTypeView.getFactname())!=null&&factMapper.findFactByFactname(taximeterTypeView.getFactname()).size()>0){
                    Fact fact = factMapper.findFactByFactname(taximeterTypeView.getFactname()).get(0);
                    taximeterTypeView.setFactid(fact.getId());
                }
                if(taximeterTypeMapper.updateTaximeterType(taximeterTypeView)){
                    return "成功";
                }else {
                    return "失败";
                }
            }
        }else if(taximeterTypes.size()>1){
            return "计价器型号重复";
        }else {
            if(taximeterTypeMapper.updateTaximeterType(taximeterTypeView)){
                return "成功";
            }else {
                return "失败";
            }
        }
    }



    @Override
    public String deleteTaximeterType(Integer id) {
        if (taximeterMapper.findTaximeterByJjqtypeid(id).size() > 0 || paramFileMapper.findParamFileByJjqtypeid(id).size() > 0) {
            return "该计价器类型已被引用";
        } else {
            if (taximeterTypeMapper.deleteTaximeterType(id)) {
                return "成功";
            } else {
                return "失败";
            }
        }
    }
}
